package com.blb.ssm.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.ssm.controller.base.BaseController;
import com.blb.ssm.entity.Menu;
import com.blb.ssm.entity.Role;
import com.blb.ssm.entity.User;
import com.blb.ssm.service.IMenuService;
import com.blb.ssm.service.IUserService;
import com.blb.ssm.utils.PageEntity;
import com.blb.ssm.utils.ResponseEntity;
import com.blb.ssm.utils.ShiroUtils;
import com.blb.ssm.utils.UUIDUtils;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huahao
 * @since 2019-01-15
 */
@Controller
@RequestMapping("/a/user")
public class UserController extends BaseController {
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IMenuService menuService;
	
	@RequiresPermissions("user:list")
	@RequestMapping("listPage")
	public String listPage(String id, Model model){
		List<Menu> menus = menuService.list(new QueryWrapper<Menu>().eq("pid", id).orderByAsc("seq"));
		model.addAttribute("menus", menus);
		return disPlay(ADMIN, USER, "user_list");
	}

	/**
	 * 分页数据
	 * @param page
	 * @param limit
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("list")
	public PageEntity userList(int page, int limit, User user){
		Page<User> page2 = new Page<User>(page, limit);
		QueryWrapper<User> queryWrapper = new QueryWrapper<>(new User());
		if (user!=null){
			if (!StringUtils.isEmpty(user.getUserName())) queryWrapper.like("user_name", user.getUserName());
			if (!StringUtils.isEmpty(user.getPassword())) queryWrapper.like("password", user.getPassword());
		}
		return new PageEntity(userService.page(page2, queryWrapper));
	}
	
	/**
	 * 保存
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("save")
	public ResponseEntity addUser(User user){
		User user2 = userService.getUserByUserName(user.getUserName());
		if (user2!=null) {
			return error("用户名已存在");
		}
		user.setId(UUIDUtils.getID());
		user.setSalt(UUIDUtils.getID());
		ShiroUtils.encPass(user);
		user.setCreateTime(new Date());
		userService.save(user);
		return success();
	}
	
	/**
	 * 删除（支持批量删除）
	 * @param users
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public ResponseEntity delete(@RequestBody ArrayList<User> users){
		try {
			List<String> list = new ArrayList<String>();
			for (User user : users) {
				if ("root".equals(user.getUserName())) {
					throw new Exception("root账号不能被删除");
				}
				list.add(user.getId());
			}
			userService.removeByIds(list);
		} catch (Exception e) {
			e.printStackTrace();
			return error(e.getMessage());
		}
		return success();
	}
	
	/**
	 * 跳转修改界面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("updatePage")
	public String adminPage(String id, Model model){
		User user = userService.getById(id);
		model.addAttribute("user", user);
		return disPlay(ADMIN, USER, "user_update");
	}
	
	/**
	 * 修改
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public ResponseEntity updateUser(User user){
		ShiroUtils.encPass(user);
		user.setUpdateTime(new Date());
		userService.updateById(user);
		return success();
	}
	
	/**
	 * 设置角色
	 * @param id
	 * @param roles
	 * @return
	 */
	@ResponseBody
	@RequestMapping("setRole")
	public ResponseEntity setRole(String id, @RequestBody ArrayList<Role> roles){
		userService.setRole(id, roles);
		return success();
	}
}

